/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.starsource.forum.client.view;

import org.starsource.forum.client.StarForum;
import org.starsource.forum.client.StarForum.AppWindow;
import org.starsource.forum.client.domain.MessageObject;
import org.starsource.forum.client.handler.MessageUpdateHandler;
import org.starsource.forum.client.util.ImageFilter;
import org.starsource.forum.client.web.ForumService;
import org.starsource.forum.client.web.ForumServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

/**
 * A simple widget representing prev/next page navigation.
 */
class MessageToolbar extends Composite {
	private final ForumServiceAsync forumService = GWT.create(ForumService.class);

	@UiTemplate("MessageToolbar.ui.xml")
	interface Binder extends UiBinder<Widget, MessageToolbar> { }
	private static final Binder binder = GWT.create(Binder.class);

	@UiField Anchor uploadButton;
	@UiField Anchor deleteFileButton;
	@UiField Anchor editButton;
	@UiField Anchor deleteButton;

	private MessageObject messageObject;

	public void setMessageObject(MessageObject messageObject) {
		this.messageObject = messageObject;
	}

	private MessageUpdateHandler messageUpdateHandler;

	public void setMessageUpdateHandler(MessageUpdateHandler messageUpdateHandler) {
		this.messageUpdateHandler = messageUpdateHandler;
	}

	public MessageToolbar() {
		initWidget(binder.createAndBindUi(this));
	}

	@UiHandler("browseButton")
	void onBrowseClicked(ClickEvent event) {
		ImageFilter imageFilter = new ImageFilter();
		ImageBrowser imageBrowser = StarForum.getInstance().getImageBrowser();
		imageBrowser.setImageList(imageFilter.getSatisfied(messageObject.getResources()));

		StarForum.getInstance().switchWindow(AppWindow.IMAGE_BROWSER);
	}

	@UiHandler("uploadButton")
	void onUploadClicked(ClickEvent event) {
		FileUploadDialog fileUploadDialog = new FileUploadDialog(messageObject, messageUpdateHandler);
		fileUploadDialog.center();
	}

	@UiHandler("deleteFileButton")
	void onDeleteFileClicked(ClickEvent event) {
		FileDeleteDialog fileDeleteDialog = new FileDeleteDialog(messageObject, messageUpdateHandler);
		fileDeleteDialog.center();
	}

	@UiHandler("editButton")
	void onEditClicked(ClickEvent event) {
		MessageEditDialog messageEditDialog = new MessageEditDialog();
		messageEditDialog.setMessageObject(messageObject);
		messageEditDialog.setMessageUpdateHandler(messageUpdateHandler);
		messageEditDialog.center();
	}

	@UiHandler("deleteButton")
	void onDeleteClicked(ClickEvent event) {
		if (Window.confirm("您确定要删除此消息吗？") == true) {
			forumService.removeMessage(messageObject.getId(), new AsyncCallback<Void>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(Void result) {
					messageUpdateHandler.onMessageUpdate(messageObject);

				}

			});
		};

	}

}
